package com.clothing.customization.repository;

import com.clothing.customization.entity.DesignerQualification;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 设计师资质数据访问接口
 */
@Repository
public interface DesignerQualificationRepository extends JpaRepository<DesignerQualification, Long>, JpaSpecificationExecutor<DesignerQualification> {
    
    /**
     * 根据设计师ID查询资质信息
     */
    Optional<DesignerQualification> findByDesignerId(Long designerId);
    
    /**
     * 根据状态查询资质信息列表
     */
    List<DesignerQualification> findByStatus(Integer status);
    
    /**
     * 根据状态分页查询资质信息
     */
    Page<DesignerQualification> findByStatus(Integer status, Pageable pageable);
    
    /**
     * 根据条件分页查询资质信息
     */
    @Query("SELECT q FROM DesignerQualification q JOIN q.designer d WHERE " +
           "(:designerName IS NULL OR d.realName LIKE %:designerName%) AND " +
           "(:status IS NULL OR q.status = :status) AND " +
           "(:startTime IS NULL OR q.createTime >= :startTime) AND " +
           "(:endTime IS NULL OR q.createTime <= :endTime)")
    Page<DesignerQualification> findByConditions(
            @Param("designerName") String designerName,
            @Param("status") Integer status,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            Pageable pageable);
    
    /**
     * 根据条件分页查询资质信息（简化版本）
     */
    @Query("SELECT q FROM DesignerQualification q JOIN q.designer d WHERE " +
           "(:designerName IS NULL OR d.realName LIKE CONCAT('%', :designerName, '%')) AND " +
           "(:status IS NULL OR q.status = :status)")
    Page<DesignerQualification> findByConditions(
            @Param("designerName") String designerName,
            @Param("status") Integer status,
            Pageable pageable);
    
    /**
     * 统计各状态的资质数量
     */
    @Query("SELECT q.status, COUNT(q) FROM DesignerQualification q GROUP BY q.status")
    List<Object[]> countByStatusGroup();
    
    /**
     * 根据状态统计资质数量
     */
    long countByStatus(Integer status);
} 